﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;

namespace Windows.Storage
{
    [TestClass]
    public class PathIOResourcesTests
    {
        [TestMethod]
        public async Task ReadTextAsyncTest()
        {
            // basic file reading text, in all encoding types
            string utf8 = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf8.txt");
            string utf16le = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf16le.txt");
            string utf16be = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf16be.txt");
            Assert.AreEqual(utf8, utf16le);
            Assert.AreEqual(utf8, utf16be);
        }

        [TestMethod]
        public async Task ReadTextAsyncWithEncodingTest()
        {
            // reference file
            string utf8 = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf8.txt");

            // PathIO detects the actual UTF8 encoding, ignores the provided flag
            string utf8a = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf8.txt", UnicodeEncoding.Utf16BE);
            string utf8b = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf8.txt", UnicodeEncoding.Utf16LE);
            string utf8c = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf8.txt", UnicodeEncoding.Utf8);
            Assert.AreEqual(utf8, utf8a);
            Assert.AreEqual(utf8, utf8b);
            Assert.AreEqual(utf8, utf8c);

            // PathIO detects the actual UTF 16 LE encoding, ignores the provided flag
            string utf16lea = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf16le.txt", UnicodeEncoding.Utf16BE);
            string utf16leb = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf16le.txt", UnicodeEncoding.Utf16LE);
            string utf16lec = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf16le.txt", UnicodeEncoding.Utf8);
            Assert.AreEqual(utf8, utf16lea);
            Assert.AreEqual(utf8, utf16leb);
            Assert.AreEqual(utf8, utf16lec);

            // PathIO detects the actual UTF 16 BE encoding, ignores the provided flag
            string utf16bea = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf16be.txt", UnicodeEncoding.Utf16BE);
            string utf16beb = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf16be.txt", UnicodeEncoding.Utf16LE);
            string utf16bec = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf16be.txt", UnicodeEncoding.Utf8);
            Assert.AreEqual(utf8, utf16bea);
            Assert.AreEqual(utf8, utf16beb);
            Assert.AreEqual(utf8, utf16bec);

            // PathIO loads files without BOM when provided expected flag
            string utf8N = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf8.nobom.txt", UnicodeEncoding.Utf8);
            string utf16leN = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf16le.nobom.txt", UnicodeEncoding.Utf16LE);
            string utf16beN = await PathIO.ReadTextAsync("ms-appx:///Data/file1.utf16be.nobom.txt", UnicodeEncoding.Utf16BE);
            Assert.AreEqual(utf8, utf8N);
            Assert.AreEqual(utf8, utf16leN);
            Assert.AreEqual(utf8, utf16beN);
        }

        [TestMethod]
        public async Task ReadLinesAsyncTest()
        {
            // basic file reading text, in all encoding types
            IList<string> utf8 = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf8.txt");
            IList<string> utf16le = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf16le.txt");
            IList<string> utf16be = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf16be.txt");
            Assert.IsTrue(utf8.SequenceEqual(utf16le));
            Assert.IsTrue(utf8.SequenceEqual(utf16be));
        }

        [TestMethod]
        public async Task ReadLinesWithEncodingAsyncTest()
        {
            // reference file
            IList<string> utf8 = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf8.txt");

            // PathIO detects the actual UTF8 encoding, ignores the provided flag
            IList<string> utf8a = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf8.txt", UnicodeEncoding.Utf16BE);
            IList<string> utf8b = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf8.txt", UnicodeEncoding.Utf16LE);
            IList<string> utf8c = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf8.txt", UnicodeEncoding.Utf8);
            Assert.IsTrue(utf8.SequenceEqual(utf8a));
            Assert.IsTrue(utf8.SequenceEqual(utf8b));
            Assert.IsTrue(utf8.SequenceEqual(utf8c));

            // PathIO detects the actual UTF 16 LE encoding, ignores the provided flag
            IList<string> utf16lea = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf16le.txt", UnicodeEncoding.Utf16BE);
            IList<string> utf16leb = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf16le.txt", UnicodeEncoding.Utf16LE);
            IList<string> utf16lec = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf16le.txt", UnicodeEncoding.Utf8);
            Assert.IsTrue(utf8.SequenceEqual(utf16lea));
            Assert.IsTrue(utf8.SequenceEqual(utf16leb));
            Assert.IsTrue(utf8.SequenceEqual(utf16lec));

            // PathIO detects the actual UTF 16 BE encoding, ignores the provided flag
            IList<string> utf16bea = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf16be.txt", UnicodeEncoding.Utf16BE);
            IList<string> utf16beb = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf16be.txt", UnicodeEncoding.Utf16LE);
            IList<string> utf16bec = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf16be.txt", UnicodeEncoding.Utf8);
            Assert.IsTrue(utf8.SequenceEqual(utf16bea));
            Assert.IsTrue(utf8.SequenceEqual(utf16beb));
            Assert.IsTrue(utf8.SequenceEqual(utf16bec));

            // PathIO loads files without BOM when provided expected flag
            IList<string> utf8N = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf8.nobom.txt", UnicodeEncoding.Utf8);
            IList<string> utf16leN = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf16le.nobom.txt", UnicodeEncoding.Utf16LE);
            IList<string> utf16beN = await PathIO.ReadLinesAsync("ms-appx:///Data/file1.utf16be.nobom.txt", UnicodeEncoding.Utf16BE);
            Assert.IsTrue(utf8.SequenceEqual(utf8N));
            Assert.IsTrue(utf8.SequenceEqual(utf16leN));
            Assert.IsTrue(utf8.SequenceEqual(utf16beN));
        }

        [TestMethod]
        public async Task ReadBufferAsyncTest()
        {
            // basic file reading text, in all encoding types
            IBuffer butf8 = await PathIO.ReadBufferAsync("ms-appx:///Data/file1.utf8.txt");
            IBuffer butf16le = await PathIO.ReadBufferAsync("ms-appx:///Data/file1.utf16le.txt");
            IBuffer butf16be = await PathIO.ReadBufferAsync("ms-appx:///Data/file1.utf16be.txt");
            // write IBuffer to Stream and read it back using StreamReader
            string utf8 = await butf8.ReadBufferAsync();
            string utf16le = await butf16le.ReadBufferAsync();
            string utf16be = await butf16be.ReadBufferAsync();
            Assert.AreEqual(utf8, utf16le);
            Assert.AreEqual(utf8, utf16be);
        }

        // Only read tests for files in Resources
    }
}